/**
 * @author hewei
 * @version 1.0
 * @description: 1138. 字母板上的路径
 * @date 2022/11/3 19:24
 */

public class AlphabetBoardPath {

    public String alphabetBoardPath(String target) {
        int n = target.length();
        StringBuilder ans = new StringBuilder();
        char last = 'a';
        for (int i = 0; i < n; i++) {
            int[] num1 = getNum(last);
            int[] num2 = getNum(target.charAt(i));
            ans.append(process(num1[0], num1[1], num2[0], num2[1]));
            last = target.charAt(i);
        }
        return ans.toString();
    }

    public int[] getNum(Character c) {
        int temp = c - 'a';
        return new int[]{temp / 5, temp % 5};
    }

    public String process(int x, int y, int i, int j) {
        StringBuilder res = new StringBuilder();
        boolean flag1 = false;
        boolean flag2 = false;
        if (x == 5 && y == 0) {
            x = 4;
            flag1 = true;
        }
        if (i == 5 && j == 0) {
            i = 4;
            flag2 = true;
        }
        int a = i - x;
        int b = j - y;
        if (b > 0) {
            while (b-- > 0) {
                res.append("R");
            }
        } else if (b < 0) {
            b = -b;
            while (b-- > 0) {
                res.append("L");
            }
        }
        if (a > 0) {
            while (a-- > 0) {
                res.append("D");
            }
        } else if (a < 0) {
            a = -a;
            while (a-- > 0) {
                res.append("U");
            }
        }
        if (flag1 && flag2) {

        } else if (flag1) {
            res.insert(0, "U");
        } else if (flag2) {
            res.append("D");
        }
        res.append("!");
        return res.toString();
    }
}
